<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Controller;
use think\Db;
use think\Request;
use think\facade\Env;
use think\facade\Session;
// use app\admin\Model\Gkdata;
class Ptzq extends Base
{
    public function index()
    {
        $data=Db::table('ptzq')->paginate(10);
        $this->assign('data',$data);
        return $this->fetch();
    }
    public function lookpt(){
        $request=new request();
        $ptid=$request->get('id');
        if($ptid!=0){
            $data=Db::table('ptzq')->where('ptid',$ptid)->paginate(10);
        }else{
            $this->error('该商品还没有添加拼团专区','product/index');
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }
    public function search()
    {
        $request=new request();
        $sname=$request->post('sname');
        $ptid=(int)$sname;
        // dump($ptid);exit;
        if(empty($ptid)){
            $data=Db::table('ptzq')->paginate(10);
        }else{
            $data=Db::table('ptzq')->where('ptid',$ptid)->paginate(10);
        }
        $this->assign('data',$data);
        return $this->fetch('index');
    }

    // 进入
    public function addpage(){
        $yhtype1=Db::table('yhtype1')->select();
        $request=new request();
        $pid=$request->param('pid');
        $pname=$request->param('pname');
        $grade=$request->param('grade');
        $this->assign('pid',$pid);
        $this->assign('pname',$pname);
        $this->assign('yhtype1',$yhtype1);
        $this->assign('grade',$grade);
        return $this->fetch();
    }
    // 添加
    public  function addad(){
        // $adxm1=Session::get('adxm');
        $request=new request();
        $ptname = $request->post("ptname");
        $pid = $request->post("pid");
        $pname = $request->post("pname");
        $grade = $request->post("grade");
        $ptstartime = $request->post("ptstartime");
        $ptstartime = strtotime($ptstartime);
        $ptendtime = $request->post("ptendtime");
        $ptendtime = strtotime($ptendtime);
        $yid = $request->post("yid");
        $data1=[
            "ptname"=>$ptname,
            "ptstartime"=>$ptstartime,
            "ptendtime"=>$ptendtime,
            "yid"=>$yid
        ];
        $re=Db::name('ptzq')->insert($data1);
        if($re){
            $ptid= Db::name('ptzq')->getLastInsID();
            $d=[
                "pid"=>$pid,
                "ptid"=>$ptid
                ];
            $r=Db::name('product')->update($d);
            if($r){
                if($grade=="1"){
                    $this->success('新增拼团成功,正在跳转添加属性等级信息', 'psx/addpage?pid='.$pid.'&pname='.$pname);
                }else{
                    $this->success('新增拼团成功', 'ptzq/index');
                }
            }
        }else{
            return $this->error('新增拼团失败');
        }
    }
    // 进入更改优惠
    public function editpage(){
        $request=new request();
        $id= intval($request->get("id"));
        $data=Db::name('ptzq')->where('ptid',$id)->find();       
        $this->assign('data',$data);
        return $this->fetch();
    }
    // 审核优惠
    
    // 更改优惠
    public  function editad(){
        $request=new request();
        $data1=[];
        $ptid=$request->post('ptid');
        $data1['ptid']=$ptid;
        $data=Db::name('ptzq')->where('ptid',$ptid)->find();
        $ptname = $request->post("ptname");
        if($ptname!=$data['ptname']){
            $data1['ptname']=$ptname;
        }
        $ptmaxnum = $request->post("ptmaxnum");
        if($ptmaxnum!=$data['ptmaxnum']){
            $data1['ptmaxnum']=$ptmaxnum;
        }
        $ptstartime = $request->post("ptstartime");
        $ptstartime = strtotime($ptstartime);
        if($ptstartime!=$data['ptstartime']){
            $data1['ptstartime']=$ptstartime;
        }
        $ptendtime = $request->post("ptendtime");
        $ptendtime = strtotime($ptendtime);
        if($ptendtime!=$data['ptendtime']){
            $data1['ptendtime']=$ptendtime;
        } 

        $da=Db::name('ptzq')->update($data1);
        if($da){
            $this->success('更新拼团成功', 'ptzq/index');
        }else{
            return $this->error('更新拼团失败');
        }

    }
    // 删除用户
    public function delptzq()
    {

        $request=new request();
        $ptid=$request->get("id");
        $da=Db::name('ptzq')->delete($ptid);
        if($da){
            $this->success('删除成功', 'ptzq/index');
        }
        else{
            return $this->error('删除失败');
        }
    }




}
